/**
 * @file    filter.h
 * @brief   算法库-滤波算法
 * @author  Liuwei
 * @version 1.0.1
 * @date    2023-04-26
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-04-26 <td>1.0.1     <td>Liuwei   <td>首次创建
 * </table>
 */

#ifndef __FILTER_H_
#define __FILTER_H_

/* file includes -------------------------------------------------------------*/
#include <stdint.h>

/* cplusplus -----------------------------------------------------------------*/
#ifdef __cplusplus
extern "C" {
#endif

/* macro/define/typedef ------------------------------------------------------*/
#define PI 3.14159265

#define FILTER_BYPASS 0  // 不滤波
#define FILTER_1ST_LP 1 // 一阶低通
#define FILTER_1ST_HP 2 // 一阶高通
#define FILTER_2ND_LP 3 // 2阶低通
#define FILTER_2ND_HP 4 // 2阶高通
#define FILTER_2ND_BP 5 // 2阶带通
#define FILTER_2ND_BS 6 // 2阶带阻

typedef struct 
{
	uint8_t jump_flag;     /*!<跳变标志位 */
	uint16_t buf_size;	    /*!<缓冲区大小 */
	uint16_t now_idx;	    /*!<当前位置 */
	uint16_t calc_num;		/*!<有效数据个数 */
	uint16_t jump;	        /*!<跳变差值 */
    uint16_t *data_buf;     /*!<滤波缓冲区 */
}filter_type;

typedef struct
{
    int type;
    float fl;
}fir_type;  // 滤波器类型

typedef struct
{
    fir_type type;
    float ts; // 采样间隔时间
    float dbgain; // 增益
    float q;  // 品质因子
    float amp; // 幅值
    float a[3]; // a0,a1,a2
    float b[3]; // b0,b1,b2
    float x[3]; // x(n),x(n-1),x(n-2)
    float y[2]; // y(n-1),y(n-2)
}lp_filter_type;

typedef struct kfp
{
	float Q;
	float R;
	float last;
	float now;
	float out;
	float gain;
}kfp_coeff_type;
/* variables -----------------------------------------------------------------*/

/*******************************************************************************
 * public API List
 ******************************************************************************/
void filter_login(filter_type *data_obj , uint16_t *data_buf , uint16_t buf_size , uint16_t value);
uint16_t filter_calc(filter_type *data_obj , uint16_t data);
void filter_lp_init(lp_filter_type *fir, int zero);
float filter_lp_calc(lp_filter_type *fir, float smp_in);
float filter_kalman_calc(kfp_coeff_type *kfp , float src);
float filter_lp_calc_jump(lp_filter_type *fir, float smp_in);
uint16_t filter_deep_calc(filter_type *data_obj , uint16_t data);

#ifdef __cplusplus
}
#endif

#endif  /* #ifndef __FILTER_H_ */
/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/
